#!/bin/bash

set -e;

source ${SDK_SRC_ROOT_DIR}/.config

echo "RTSMART_SRC_DIR ${RTSMART_SRC_DIR}"

function modify_rtsmart_code()
{
	# modify rtt console configure
	{
		local RTT_DRV_UART="${RTSMART_SRC_DIR}/kernel/bsp/maix3/drivers/interdrv/uart/drv_uart.c"

		local RTT_UART_BAUD="$( printf '%d\n' $[ ${CONFIG_RTT_CONSOLE_BAUD} ])"
		local RTT_UART_ADDR="0x9140${CONFIG_RTT_CONSOLE_ID}000UL"
		local RTT_UART_IRQ="0x1${CONFIG_RTT_CONSOLE_ID}"
		local RTT_UART_STR="$( printf 'uart%d\n' $[ ${CONFIG_RTT_CONSOLE_ID} ])"
		local RTT_SERIAL_STR="$( printf 'serial%d\n' $[ ${CONFIG_RTT_CONSOLE_ID} ])"

		sed -i "s/define *UART_DEFAULT_BAUDRATE.*$/define UART_DEFAULT_BAUDRATE ${RTT_UART_BAUD}/g" ${RTT_DRV_UART}
		sed -i "s/define *UART_ADDR.*$/define UART_ADDR ${RTT_UART_ADDR}/g" ${RTT_DRV_UART}
		sed -i "s/define *UART_IRQ.*$/define UART_IRQ ${RTT_UART_IRQ}/g" ${RTT_DRV_UART}
		sed -i "s/uart1/${RTT_UART_STR}/g" ${RTT_DRV_UART}
		sed -i "s/serial1/${RTT_SERIAL_STR}/g" ${RTT_DRV_UART}
	}

	# modify rtt memory releated
	# {
	# 	MEM_CFG_RTT_LDS="${RTSMART_SRC_DIR}/kernel/bsp/maix3/link.lds"
	# 	MEM_CFG_RTT_RAMEND="${RTSMART_SRC_DIR}/kernel/bsp/maix3/board/board.h"

	# 	local RTT_SYS_BASE="$( printf '0x%x\n' $[ ${CONFIG_MEM_RTSMART_BASE} + 0x20000 ] )"
	# 	local RTT_SYS_SIZE="$( printf '%d\n' $[ ( ( ${CONFIG_MEM_RTSMART_SIZE} - 0x20000 ) / 1024 ) - 1 ] )"
	# 	local RTT_RAM_END="$( printf '0x%x\n' $[ ${RTT_SYS_BASE} + ( ${RTT_SYS_SIZE} * 1024 ) - 4096 ] )"

	# 	echo "Modify file: ${MEM_CFG_RTT_LDS}"
	# 	sed -i "s/. =.*rt-smart link start address.*$/. = ${RTT_SYS_BASE};    \/\*rt-smart link start address\*\//g" ${MEM_CFG_RTT_LDS}
	# 	sed -i "s/SRAM : ORIGIN.*$/SRAM : ORIGIN = ${RTT_SYS_BASE}, LENGTH = ${RTT_SYS_SIZE}K/g" ${MEM_CFG_RTT_LDS}

	# 	echo "Modify file: ${MEM_CFG_RTT_RAMEND}"
	# 	sed -i "s/#define RAM_END.*$/#define RAM_END ${RTT_RAM_END}/g" ${MEM_CFG_RTT_RAMEND}
	# 	sed -i "s/#define RT_HEAP_SIZE.*$/#define RT_HEAP_SIZE ${CONFIG_MEM_RTSMART_HEAP_SIZE}/g" ${MEM_CFG_RTT_RAMEND}
	# }
}

modify_rtsmart_code;
